import {reqTradeInfo,reqfindUserAddressList,reqSubmitOrder,reqCreateNative} from "@/api"

//准备actions一用于响应组件中的动作
const actions ={
    async tradeInfo({commit}){
        let result = await reqTradeInfo()
        if(result.code == 200){
            commit("TRADEINFO",result.data)
            return "ok"
        }else{
            return Promise.reject(new Error("faile"))
        }
    },
    // 获取用户地址信息
    async findUserAddressList({commit}){
        let result = await reqfindUserAddressList()
        if(result.code == 200){
            commit("REQFINDUSERADDRESSLIST",result.data)
            return "ok"
        }else{
            return Promise.reject(new Error("faile"))
        }
    },
    //提交订单
    async submitOrder({commit},data){
        let result = await reqSubmitOrder(data)
        if(result.code == 200){
            commit("SUBMITORDER",result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error("faile"))
        }
    },
    //获取订单支付信息
    async createNative({commit},data){
        let result = await reqCreateNative(data)
        if(result.code == 200){
            commit("CREATENATIVE",result.data)
        }
    }
}
//准备mutations.一用于操作数据(state)
const mutations ={
    TRADEINFO(start,tradeInfo){
        start.tradeInfo = tradeInfo
    },
    // 用户地址信息
    REQFINDUSERADDRESSLIST(state,findUserAddressList){
        state.findUserAddressList = findUserAddressList
    },
    //订单号
    SUBMITORDER(state,traderNo){
        state.traderNo = traderNo
    },
    //获取订单支付信息
    CREATENATIVE(start,createNative){
        start.createNative = createNative
    }
}
//准备state一用于存储数据
const state ={
    tradeInfo:{},
    findUserAddressList:[],
    traderNo:'',
    createNative:{}
}
//getters:理解为计算属性，用于简化仓库数据，让组件获取仓库的数据更加方便
const getters = {
    detailArrayList(){
        return state.tradeInfo.detailArrayList
    }
}

//创建并暴露store
export default {
state,
mutations,
actions,
getters,
}